package cn.itsource.service.service.impl;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.service.IBaiduAiAuditService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.LoginContext;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.service.domain.Product;
import cn.itsource.service.domain.ProductDetail;
import cn.itsource.service.mapper.ProductDetailMapper;
import cn.itsource.service.mapper.ProductMapper;
import cn.itsource.service.service.IProductService;
import cn.itsource.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;


    @Override
    public void save(Product product) {
        super.save(product);;
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null){
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }
    }

    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null){
            productDetail.setProductId(product.getId());
            productDetailMapper.updateProductDetailByProductId(productDetail);
        }
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
        productDetailMapper.delProductDetailByProductId(id);
    }


    @Override
    public void onsale(Long[] ids, HttpServletRequest request) {
        //获取登录信息对象
        Logininfo logininfo = LoginContext.getLoginUser(request);
        //获取审核人
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            //1.上架之前对文本和图片做审核
            Boolean textBoo = baiduAiAuditService.textAudit(product.getName());
            Boolean imageBoo = baiduAiAuditService.imageAudit(product.getResources());
            if(textBoo && imageBoo){
                //2.审核通过：修改状态 + 添加上架的审核成功日志
                product.setOnsaletime(new Date());
                product.setState(1L);
                productMapper.update(product);
            } else {
                throw new BusinessException("存在低俗辱骂不合规");
            }
        }
    }

    @Override
    public void offsale(Long[] ids) {
        Date offsaletime = new Date();
        productMapper.offsale(offsaletime,ids);
    }
}
